package com.huyong.controller;

import com.huyong.entity.UserLogin;
import com.huyong.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class LoginController {
    @Autowired
    private LoginService loginService;

    @GetMapping("/hello")
    @ResponseBody
    public String test(){
        return "hello";
    }

    //登录
    @GetMapping("/login")
    @ResponseBody
    public String login(String userName,String password){
        try {
            boolean result = loginService.login(userName, password);
            if (result){
                return "UserList.html";
            }
        }catch (Exception e){
            return "系统错误，请稍后再试";
        }
        return "用户名或密码错误";
    }

    //注册
    @PostMapping("/register")
    @ResponseBody
    public String register(String userName,String password,String code){
        UserLogin userRegister = new UserLogin();
        userRegister.setUserName(userName).setPassword(password).setCode(code);
        boolean result = loginService.register(userRegister);
        if(result){
            return "success";
        }else {
            return "false";
        }
    }

    //修改密码
    @PostMapping("/forget")
    @ResponseBody
    public String forget(String userName,String password,String code){
        UserLogin userForget = new UserLogin();
        userForget.setUserName(userName).setPassword(password).setCode(code);
        boolean result = loginService.forget(userForget);
        if(result){
            return "success";
        }else {
            return "false";
        }
    }
}
